package com.zy.control;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zy.entity.Admin;
import com.zy.entity.Course;
import com.zy.entity.Paging;
import com.zy.entity.Speaker;
import com.zy.entity.Subject;
import com.zy.entity.Video;
import com.zy.entity.VideoEx;
import com.zy.service.AdminService;
@Controller
@Component
@RequestMapping("/admin")
public class AdminControl {
	@Autowired
	private AdminService as;
	public AdminService getAs() {
		return as;
	}
	public void setAs(AdminService as) {
		this.as = as;
	}
	//分页查询视频
	@RequestMapping("/pagingVideo")
	public ModelAndView pagingVideo(Paging pg,HttpServletRequest request){
		ModelAndView mv =new ModelAndView();
		
		Map<String, Object> map = as.selectLimitVideo(pg);
		int numCount = (int)map.get("countVideo");
		pg.setNumCount(numCount);
		pg.setCurrentPage(pg.getCurrentPage());
		List<Video> vlist = (List<Video>)map.get("vlist");
		mv.addObject("pageBean", pg);
		mv.addObject("vlist", vlist);
		mv.setViewName("/WEB-INF/admin/video/video.jsp");
		return mv;
	}
	//动态查询视频
	@RequestMapping("/selectLikeVideo")
	public ModelAndView selectLikeVideo(VideoEx vd){
		ModelAndView mv =new ModelAndView();
		List<Video> selectLikeVideo = as.selectLikeVideo(vd);
		mv.addObject("vlist", selectLikeVideo);
		List<Speaker> allSpeaker = as.getAllSpeaker();
		mv.addObject("slist", allSpeaker);
		List<Course> allCourse = as.getAllCourse();
		mv.addObject("clist", allCourse);
		mv.setViewName("/WEB-INF/admin/video/video.jsp");
		return mv;
	}
	//修改主讲人
	@RequestMapping("/updateSpeaker")
	public ModelAndView updateSpeaker(Speaker sp){
		ModelAndView mv =new ModelAndView();
		as.updateSpeaker(sp);
		mv.setViewName("redirect:/admin/speaker.action");
		return mv;
	}
	//去修改主讲人
	@RequestMapping("/toUpdateSpeaker")
	public ModelAndView toUpdateSpeaker(Speaker sp){
		ModelAndView mv =new ModelAndView();
		Speaker selectSpeakerById = as.selectSpeakerById(sp);
		mv.addObject("slist", selectSpeakerById);
		mv.setViewName("/WEB-INF/admin/speaker/update_speaker.jsp");
		return mv;
	}
	//删除主讲人
	@RequestMapping("/deleteSpeaker")
	public ModelAndView deleteSpeaker(Speaker sp){
		ModelAndView mv =new ModelAndView();
		as.deleteSpeakerById(sp);
		
		mv.setViewName("redirect:/admin/speaker.action");
		return mv;
	}
	//添加主讲人
	@RequestMapping("/addSpeaker")
	public ModelAndView addSpeaker(Speaker sp){
		ModelAndView mv =new ModelAndView();
		as.addSpeaker(sp);
		mv.setViewName("redirect:/admin/speaker.action");
		return mv;
	}
	//去添加主讲人
	@RequestMapping("/toAddSpeaker")
	public ModelAndView toAddSpeaker(Speaker sp){
		ModelAndView mv =new ModelAndView();
		mv.setViewName("/WEB-INF/admin/speaker/add_speaker.jsp");
		return mv;
	}
	//主讲人管理
	@RequestMapping("/speaker")
	public ModelAndView speaker(){
		ModelAndView mv =new ModelAndView();
		List<Speaker> allSpeaker = as.getAllSpeaker();
		mv.addObject("slist", allSpeaker);
		mv.setViewName("/WEB-INF/admin/speaker/speaker.jsp");
		return mv;
	}
	//修改课程
	@RequestMapping("/updateCourse")
	public ModelAndView updateCourse(Course cu){
		ModelAndView mv =new ModelAndView();
		as.updateCourse(cu);
		mv.setViewName("redirect:/admin/course.action");
		return mv;
	}
	//去修改课程
	@RequestMapping("/toUpdateCourse")
	public ModelAndView toUpdateCourse(Course cu){
		ModelAndView mv =new ModelAndView();
		List<Subject> allSubject = as.getAllSubject();
		mv.addObject("sublist", allSubject);
		Course courseById = as.selectCourseById(cu);
		mv.addObject("clist", courseById);
		mv.setViewName("/WEB-INF/admin/course/update_course.jsp");
		return mv;
	}
	//删除课程
	@RequestMapping("/deleteCourse")
	public ModelAndView deleteCourse(Course cu){
		ModelAndView mv =new ModelAndView();
		as.deleteCourseById(cu);
		mv.setViewName("course.action");
		return mv;
	}
	//添加课程
	@RequestMapping("/addCourse")
	public ModelAndView addCourse(Course cu){
		ModelAndView mv =new ModelAndView();
		as.addCourse(cu);
		mv.setViewName("redirect:/admin/course.action");
		return mv;
	}
	//去添加课程页面
	@RequestMapping("/toAddCourse")
	public ModelAndView toAddCourse(){
		ModelAndView mv =new ModelAndView();
		List<Subject> allSubject = as.getAllSubject();
		mv.addObject("sublist", allSubject);
		mv.setViewName("/WEB-INF/admin/course/add_course.jsp");
		return mv;
	}
	//课程列表
	@RequestMapping("/course")
	public ModelAndView course(){
		ModelAndView mv =new ModelAndView();
		List<Course> allCourse = as.getAllCourse();
		mv.addObject("clist", allCourse);
		mv.setViewName("/WEB-INF/admin/course/course.jsp");
		return mv;
	}
	//修改视频
	@RequestMapping("/updateVideo")
	public ModelAndView updateVideo(Video vd){
		ModelAndView mv =new ModelAndView();
		as.updateVideo(vd);
		mv.setViewName("pagingVideo.action");
		return mv;
	}
	//去修改视频
	@RequestMapping("/toUpdete_video")
	public ModelAndView toUpdete_video(Video vd){
		ModelAndView mv=new ModelAndView();
		System.out.println(vd.getId());
		List<Speaker> allSpeaker = as.getAllSpeaker();
		mv.addObject("slist", allSpeaker);
		Video selectVideoById = as.selectVideoById(vd);
		mv.addObject("vlist", selectVideoById);
		mv.setViewName("/WEB-INF/admin/video/update_video.jsp");
		return mv;
	}
	//删除视频
	@RequestMapping("/deleteIdVideo")
	public ModelAndView deleteIdVideo(Video vd){
		ModelAndView mv=new ModelAndView();
		as.deleteIdVideo(vd);
		mv.setViewName("redirect:/admin/pagingVideo.action");
		return mv;
	}
	//批量删除视频
	@RequestMapping("/deleteBatcheVideo")
	public ModelAndView deleteBatcheVideo(String vid){
		ModelAndView mv=new ModelAndView();
		String[] split = vid.split(",");
		int[] id=new int[split.length];
		for(int i=0;i<id.length;i++){
			id[i]=Integer.parseInt(split[i]);
		}
		as.deleteBatcheVideo(id);
		mv.setViewName("redirect:/admin/pagingVideo.action");
		return mv;
	}
	//去添加视频页面
	@RequestMapping("/toAddVideo")
	public ModelAndView toAddVideo(){
		ModelAndView mv=new ModelAndView();
		List<Speaker> allSpeaker = as.getAllSpeaker();
		mv.addObject("slist", allSpeaker);
		List<Course> allCourse = as.getAllCourse();
		mv.addObject("clist", allCourse);
		mv.setViewName("/WEB-INF/admin/video/add_video.jsp");
		return mv;
	}
	//添加视频
	@RequestMapping("/addVideo")
	public ModelAndView addVideo(Video vd){
		ModelAndView mv=new ModelAndView();
		as.addVideo(vd);
		mv.setViewName("redirect:/admin/pagingVideo.action");
		return mv;
	}
	
	//去登录页面
	@RequestMapping("/toLogin")
	public ModelAndView toLogin(){
		ModelAndView mv=new ModelAndView();
		
		mv.setViewName("/login.jsp");
		return mv;
	}
	//登录
	@RequestMapping("/login")
	public ModelAndView login(Admin an){
		ModelAndView mv=new ModelAndView();
		if(as.login(an)){
			mv.setViewName("pagingVideo.action");
			return mv;
		}
		mv.setViewName("toLogin.action");
		return mv;
	}
	//视频列表
	@RequestMapping("/video")
	public ModelAndView video(){
		ModelAndView mv=new ModelAndView();
		List<Video> allVideo = as.getAllVideo();
		mv.addObject("vlist", allVideo);
		List<Speaker> allSpeaker = as.getAllSpeaker();
		mv.addObject("slist", allSpeaker);
		List<Course> allCourse = as.getAllCourse();
		mv.addObject("clist", allCourse);
		mv.setViewName("/WEB-INF/admin/video/video.jsp");
		return mv;
	}
}
